# 1 "../Codes/Y36_LCD_horizontal/temperature_control_sub_module/Kalman.c"
# 1 "E:\\APT_Landscape_mode\\APT32F1023_New\\Source//"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "../Codes/Y36_LCD_horizontal/temperature_control_sub_module/Kalman.c"






# 1 "../Codes/Y36_LCD_horizontal/temperature_control_sub_module/Kalman.h" 1
# 10 "../Codes/Y36_LCD_horizontal/temperature_control_sub_module/Kalman.h"
typedef struct
{
    float LastP;
    float Now_P;
    float out;
    float Kg;
    float Q;
    float R;
}KFP_S;
extern KFP_S KFP_ADC_RTD;
extern KFP_S KFP_ADC_Probe1;
extern KFP_S KFP_ADC_Probe2;


float kalmanFilter(KFP_S *kfp,float input);
float MedianFilter(float xn);
float MedianFilterA(float xn);
float MedianFilterB(float xn);
# 8 "../Codes/Y36_LCD_horizontal/temperature_control_sub_module/Kalman.c" 2

# 1 "d:\\c-sky\\cdkrepo\\toolchain\\ckv2elfminilib\\v3.10.32\\r\\lib\\gcc\\csky-elfabiv2\\6.3.0\\include\\stdint.h" 1 3 4
# 9 "d:\\c-sky\\cdkrepo\\toolchain\\ckv2elfminilib\\v3.10.32\\r\\lib\\gcc\\csky-elfabiv2\\6.3.0\\include\\stdint.h" 3 4
# 1 "d:\\c-sky\\cdkrepo\\toolchain\\ckv2elfminilib\\v3.10.32\\r\\csky-elfabiv2\\sys-include\\stdint.h" 1 3 4
# 33 "d:\\c-sky\\cdkrepo\\toolchain\\ckv2elfminilib\\v3.10.32\\r\\csky-elfabiv2\\sys-include\\stdint.h" 3 4

# 33 "d:\\c-sky\\cdkrepo\\toolchain\\ckv2elfminilib\\v3.10.32\\r\\csky-elfabiv2\\sys-include\\stdint.h" 3 4
typedef signed char int8_t;
typedef short int int16_t;
typedef int int32_t;
typedef int __int32_t;
__extension__
typedef long long int int64_t;



typedef unsigned char uint8_t;
typedef unsigned short int uint16_t;

typedef unsigned int uint32_t;
typedef unsigned int __uint32_t;


__extension__
typedef unsigned long long int uint64_t;





typedef signed char int_least8_t;
typedef short int int_least16_t;
typedef int int_least32_t;
__extension__
typedef long long int int_least64_t;


typedef unsigned char uint_least8_t;
typedef unsigned short int uint_least16_t;
typedef unsigned int uint_least32_t;
__extension__
typedef unsigned long long int uint_least64_t;





typedef signed char int_fast8_t;
typedef int int_fast16_t;
typedef int int_fast32_t;
__extension__
typedef long long int int_fast64_t;


typedef unsigned char uint_fast8_t;
typedef unsigned int uint_fast16_t;
typedef unsigned int uint_fast32_t;
__extension__
typedef unsigned long long int uint_fast64_t;




typedef int intptr_t;


typedef unsigned int uintptr_t;



__extension__
typedef long long int intmax_t;
__extension__
typedef unsigned long long int uintmax_t;
# 10 "d:\\c-sky\\cdkrepo\\toolchain\\ckv2elfminilib\\v3.10.32\\r\\lib\\gcc\\csky-elfabiv2\\6.3.0\\include\\stdint.h" 2 3 4
# 10 "../Codes/Y36_LCD_horizontal/temperature_control_sub_module/Kalman.c" 2



# 12 "../Codes/Y36_LCD_horizontal/temperature_control_sub_module/Kalman.c"
KFP_S KFP_ADC_RTD = {100, 0, 0, 0, 40, 450};
KFP_S KFP_ADC_Probe1 = {100, 0, 0, 0, 40, 400};
KFP_S KFP_ADC_Probe2 = {100, 0, 0, 0, 40, 400};
# 23 "../Codes/Y36_LCD_horizontal/temperature_control_sub_module/Kalman.c"
float kalmanFilter(KFP_S *kfp,float input)
{

    kfp->Now_P = kfp->LastP + kfp->Q;


    kfp->Kg = kfp->Now_P / (kfp->Now_P + kfp->R);

    kfp->out = kfp->out + kfp->Kg * (input -kfp->out);

    kfp->LastP = (1-kfp->Kg) * kfp->Now_P;

    return kfp->out;
}



float MedianFilter(float xn)
{
    static int index = 0;
    static float buffer[20];
    int i = 0;
    int j = 0;
    float temp = 0;

    buffer[index] = xn;
    index++;
    if(index >= 20) index = 0;

    for (j = 0; j < (20 - 1); j++)
    {
        for (i = 0; i < ((20 - 1) - j); i++)
        {
            if (buffer[i] > buffer[i + 1])
            {
                temp = buffer[i];
                buffer[i] = buffer[i + 1];
                buffer[i + 1] = temp;
            }
        }
    }

    return ((buffer[(20 -1)/2 + 1] + buffer[(20 -1)/2] + buffer[(20 -1)/2 - 1]) / 3);
}
float MedianFilterA(float xn)
{
    static int index = 0;
    static float buffer[20];
    int i = 0;
    int j = 0;
    float temp = 0;

    buffer[index] = xn;
    index++;
    if(index >= 20) index = 0;

    for (j = 0; j < (20 - 1); j++)
    {
        for (i = 0; i < ((20 - 1) - j); i++)
        {
            if (buffer[i] > buffer[i + 1])
            {
                temp = buffer[i];
                buffer[i] = buffer[i + 1];
                buffer[i + 1] = temp;
            }
        }
    }

    return ((buffer[(20 -1)/2 + 1] + buffer[(20 -1)/2] + buffer[(20 -1)/2 - 1]) / 3);
}
float MedianFilterB(float xn)
{
    static int index = 0;
    static float buffer[20];
    int i = 0;
    int j = 0;
    float temp = 0;

    buffer[index] = xn;
    index++;
    if(index >= 20) index = 0;

    for (j = 0; j < (20 - 1); j++)
    {
        for (i = 0; i < ((20 - 1) - j); i++)
        {
            if (buffer[i] > buffer[i + 1])
            {
                temp = buffer[i];
                buffer[i] = buffer[i + 1];
                buffer[i + 1] = temp;
            }
        }
    }

    return ((buffer[(20 -1)/2 + 1] + buffer[(20 -1)/2] + buffer[(20 -1)/2 - 1]) / 3);
}
